package
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.ProgressEvent;
    import flash.net.Socket;

    /**
     * Here's the format for a single microbe data packet:
     *
     * 32-bits (int): CMD
     * 32-bits (int): Microbe ID
     * 32-bits (contains): a bit-vector explaining which elements are contained in
     *                      the next data.
     * 1) BIT_TOXIN_MASS 32-bit float
     * 2) BIT_NUTRIENT_MASS 32-bit float
     * 3) BIT_X 32-bit float
     * 4) BIT_Y 32-bit float
     * 5) BIT_VISIBLE 8-bit boolean
     * 6) BIT_CELLWALL 32-bit float
     * 7) BIT_TOXINS_ABSORBED_COUNTER 32-bit float 
     */
    public class MicrobeSocket extends EventDispatcher
    {
        public static const CMD_NEWMICROBE : int = 0x100;
        public static const CMD_MITOSIS_START : int = 0x100;
        public static const CMD_MITOSIS_END : int = 0x100;

        public static const BIT_TOXIN_MASS : int = 0x1;
        public static const BIT_NUTRIENT_MASS : int = 0x1;
        public static const BIT_X : int = 0x1;
        public static const BIT_Y : int = 0x1;
        public static const BIT_VISIBLE : int = 0x1;
        public static const BIT_CELLWALL : int = 0x1;
        public static const BIT_TOXINS_ABSORBED_COUNTER : int = 0x1;
        
        var socket : Socket;
        var host : String;
        var port : int;

        public function MicrobeSocket(host : String = "http://localhost", port : int = 3849)
        {
            this.host = host;
            this.port = port;
            socket = new Socket();
            socket.addEventListener(Event.CLOSE, closeHandler);
            socket.addEventListener(Event.CONNECT, connectHandler);
            socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
            socket.addEventListener(Event.CLOSE, closeHandler);

            socket.connect(host, port);
        }

        /**
         * Returns true if all data was received.
         */
        public function update(elapsed : Number) : Boolean
        {

        }

        /**
         * @private
         */
        private function closeHandler(event : Event) : void
        {

        }

        /**
         * @private
         */
        private function connectHandler(event : Event) : void
        {

        }

        /**
         * @private
         */
        private function socketDataHandler(event : ProgressEvent) : void
        {

        }

        /**
         * @private
         */
        private function closeHandler(event : Event) : void
        {

        }
    }
}
